package com.xxxx.microservice.forestry.utils;

import com.supermap.analyst.spatialanalyst.InterpolationKrigingParameter;
import com.supermap.analyst.spatialanalyst.Interpolator;
import com.supermap.analyst.spatialanalyst.SearchMode;
import com.supermap.analyst.spatialstatistics.ClusteringDistributions;
import com.supermap.analyst.spatialstatistics.PatternsParameter;
import com.supermap.data.*;

public class SuperMapTest {
    public static void main(String[] args) {

        // 插值
        interpolate("xiangdm = '110117114'", "om");

        // 聚类
        clusterOutlierAnalyst("", "ph");

    }

    // 插值
    public static void interpolate(String filter, String filed) {
        Workspace m_workspace = new Workspace();
        DatasetVector datasetVector = getDatasetVector(m_workspace);
        Datasource datasource = datasetVector.getDatasource();

        String filterName = "interpolate_filter";
        DatasetVector filterVector = filterAndCreate(datasetVector, filterName, filter);

        InterpolationKrigingParameter parameter = new InterpolationKrigingParameter();
        parameter.setSearchMode(SearchMode.KDTREE_FIXED_COUNT);
        parameter.setExpectedCount(5);
        parameter.setResolution(9000);
        parameter.setBounds(filterVector.computeBounds());

        String resultName = "interpolate_result";
        datasource.getDatasets().delete(resultName);
        DatasetGrid m_datasetResult = Interpolator.interpolate(parameter, filterVector,
                filed, 1, datasource, resultName, PixelFormat.SINGLE);

        System.out.println("克吕金插值栅格结果范围：" + m_datasetResult.getBounds());

        datasource.getDatasets().delete(filterName);
        datasource.getDatasets().delete(resultName);
        m_workspace.dispose();
    }

    // 聚类
    public static void clusterOutlierAnalyst(String filter, String filed) {
        Workspace m_workspace = new Workspace();
        DatasetVector datasetVector = getDatasetVector(m_workspace);
        Datasource datasource = datasetVector.getDatasource();

        String filterName = "cluster_filter";
        DatasetVector filterVector = filterAndCreate(datasetVector, filterName, filter);

        PatternsParameter patternsParameter = new PatternsParameter();
        patternsParameter.setAssessmentFieldName(filed);

        String resultName = "cluster_result";
        datasource.getDatasets().delete(resultName);
        DatasetVector result = ClusteringDistributions.clusterOutlierAnalyst(
                filterVector,
                datasource,
                resultName,
                patternsParameter);

        Feature feature = result.getAllFeatures().get(1);
        System.out.println("[1]局部莫兰指数：" + feature.getValue("ALMI_MoranI"));
        System.out.println("[1]z得分：" + feature.getValue("ALMI_Zscore"));
        System.out.println("[1]P值：" + feature.getValue("ALMI_Pvalue"));
        System.out.println("[1]聚类和异常值类型：" + feature.getValue("ALMI_Type"));

        datasource.getDatasets().delete(filterName);
        datasource.getDatasets().delete(resultName);
        m_workspace.dispose();
    }

    public static DatasetVector getDatasetVector(Workspace m_workspace) {
        WorkspaceConnectionInfo conInfo = new WorkspaceConnectionInfo(
                "F:\\北京园林第三次普查\\forestry-service\\test\\omph\\LYSP.smwu");
        conInfo.setType(WorkspaceType.SMWU);
        m_workspace.open(conInfo);
        Datasource datasource = m_workspace.getDatasources().get(1);
        return (DatasetVector) datasource.getDatasets().get("ydzb_om_ph");
    }

    public static DatasetVector filterAndCreate(DatasetVector sourceVector, String targetName, String filter) {
        if (filter == null || filter.isEmpty()) {
            return sourceVector;
        }
        FieldInfo[] fieldInfos = sourceVector.getFieldInfos().toArray();
        FieldInfo[] newFieldInfos = new FieldInfo[4];
        newFieldInfos[0] = fieldInfos[3];
        newFieldInfos[1] = fieldInfos[4];
        newFieldInfos[2] = fieldInfos[5];
        newFieldInfos[3] = fieldInfos[6];
        Datasets datasets = sourceVector.getDatasource().getDatasets();
        if (datasets.contains(targetName)) {
            datasets.delete(targetName);
        }
        DatasetVectorInfo info = new DatasetVectorInfo(targetName, DatasetType.POINT);
        DatasetVector dataset = datasets.create(info);
        dataset.getFieldInfos().addRange(newFieldInfos);
        dataset.append(sourceVector.query(filter, CursorType.STATIC));
        return dataset;
    }

}
